<?php 
class Photo_model extends MY_Model {
        
	function Photo_model() {
		parent::__construct("s_photo");
	}
        
/**
     * 公用查询程序
     * $condition['society_id'] = ?  //按照社团编号（外键:用户id）搜索
     * $condition['photo_path'] = ?  //按照照片搜索
     * $condition['thumb_path'] = ?  //按照照片缩略图搜索
     * $condition['category'] = ?  //按照照片类别（1:logo  2:banner 3:社团风采展）搜索
     * $condition['sort_upload_time'] = ?  //按照照片上传时间排序
     * $condition['offset'] = ?
     * $condition['per_page'] = ?
     * @param $condition
     * @param $row_str_FLAG 仅对查询结果是单条的：若设为TRUE：单条自动转为字符串， 若设为FALSE：单条自动转为数组
     * @param $count_FLAG 若设为TRUE：计算查询结果数， 若设为FALSE：直接查询结果
     */
    function search_by_condition($condition, $row_str_FLAG = FALSE, $count_FLAG = FALSE) {
        $this->db->select('id, society_id, photo_path, thumb_path, category, upload_time');
        
    	if (isset($condition['society_id'])) {
            $this->db->where('society_id', $condition['society_id']);
        }
        
    	if (isset($condition['photo_path'])) {
            $this->db->where('photo_path', $condition['photo_path']);
        }
        
    	if (isset($condition['thumb_path'])) {
            $this->db->where('thumb_path', $condition['thumb_path']);
        }
        
    	if (isset($condition['category'])) {
            $this->db->where('category', $condition['category']);
        }

        if (!$count_FLAG) {
            if (isset($condition['sort_upload_time'])) {
                $this->db->order_by("upload_time", $condition['sort_upload_time']);
            }

            if (isset($condition['offset'])) {
                if (isset($condition['per_page'])) {
                    $this->db->limit($condition['per_page'], $condition['offset']);
                } else {
                    $this->db->limit(PER_PAGE, $condition['offset']);
                }
            }

            $query = $this->db->get('s_photo');
        	if ($query->num_rows() == 1) {
				if (!$row_str_FLAG) {
	            	return $query->result_array();
				} else {
					return $query->row_array();
            	}
			} elseif ($query->num_rows() > 1) {
				return $query->result_array();
			} else {
				return NULL;
			}
        } else {
            $this->db->from('s_photo');
            return $this->db->count_all_results();
        }
    }
	
	
    /**
     * 得到某指定的社团ID的某分类图片全部缩略图的数据并按上传时间排序
     * @param $society_id
     * @param $category
     * @return 得到所有的数据的对象数组
     */
    function get_thumb_by_user_and_category($society_id, $category) {
        $query = $this->db->query("SELECT * FROM s_photo WHERE society_id = ? AND category = ? ORDER BY upload_time DESC", array($society_id, $category));
        return $query->result_array();
    }
    
    /**
     * 得到某指定的用户ID的某分类图片全部图片的数量
     * @param $society_id
     * @param $category
     * @return 得到所有的数据的数据量
     */
	function get_upload_count_by_user_and_category($society_id, $category) {
        $query = $this->db->query("SELECT count(id) AS num FROM s_photo WHERE society_id = ? AND category = ?", array($society_id, $category));
        $data = $query->row_array();
        return $data['num'];
    }
    
}
?>